//
//  HotCarDetailViewController.m
//  CarWorld
//
//  Created by yuyu on 15/3/28.
//  Copyright (c) 2015年 CarWorldTeam. All rights reserved.
//

#import "HotCarDetailViewController.h"
#import "HotCarDetailView.h"
#import "HotSectionView.h"
#import "HotZhongShuViewController.h"
#import "HotTuPianCollectionVC.h"
#import "HotHuoDongTableViewController.h"
#import "HotLunTanTableViewController.h"
#import "HotZiXunViewController.h"
@interface HotCarDetailViewController ()
@property(nonatomic,retain)HotSectionView *hotSectionView;
@property(nonatomic,retain)UIView *rootView;
@property(nonatomic,retain)UIView *tempView;

@end

@implementation HotCarDetailViewController

- (void)dealloc
{
    [_hotSectionView release];
    [_rootView release];
    [_tempView release];
    [_mid release];
    [super dealloc];
}

- (void)loadView
{
    HotCarDetailView *detailView = [[HotCarDetailView alloc] init];
    self.view = detailView;
    self.hotSectionView = detailView.hotSectionView;
    self.rootView = detailView.rootView;
    [detailView release];
}

- (void)changeView:(UIViewController *)VC
{
    CGRect rect = self.rootView.frame;
    rect.origin.x=rect.origin.y=0;
    VC.view.frame = rect;
    [self.tempView removeFromSuperview];
    [self.rootView addSubview:VC.view];
    self.tempView = VC.view;
    [self addChildViewController:VC];
}

- (void)clickedSectionView:(HotSectionView *)hotSectionView
{
    NSInteger index = hotSectionView.keyButton.tag;
    switch (index) {
        case 0://综述
        {
            HotZhongShuViewController *hotZsVC = [[HotZhongShuViewController alloc] init];
            hotZsVC.mid = self.mid;
            [self changeView:hotZsVC];
            [hotZsVC release];
        }
            break;
        case 1://图片
        {
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            //设置每个格子的尺寸
            CGSize size  = [UIScreen mainScreen].bounds.size;
            CGFloat W = (size.width-40)/3.0;
            CGFloat H = 5*W/3.0;
            layout.itemSize = CGSizeMake(W, H);
            //设置整个collView的内边距
            CGFloat paddingY = 10;
            CGFloat paddingX = 10;
            layout.sectionInset = UIEdgeInsetsMake(paddingX, paddingY, paddingY, paddingX);
            //设置每一行之间的距离
            layout.minimumLineSpacing = paddingY;
            HotTuPianCollectionVC *hotDetailCollectionVC = [[HotTuPianCollectionVC alloc] initWithCollectionViewLayout:layout];
            
            [self changeView:hotDetailCollectionVC];
            [hotDetailCollectionVC release];
            [layout release];
        }
            break;
        case 2://资讯
        {
            HotZiXunViewController *ziXunVC = [[HotZiXunViewController alloc] init];
            [self changeView:ziXunVC];
            [ziXunVC release];
        }
            break;
        case 3://活动
        {
            HotHuoDongTableViewController *huoDongVC = [[HotHuoDongTableViewController alloc] init];
            [self changeView:huoDongVC];
            [huoDongVC release];
        }
            break;
        case 4://论坛
        {
            HotLunTanTableViewController *lunTanVC = [[HotLunTanTableViewController alloc] init];
            [self changeView:lunTanVC];
            [lunTanVC release];
        }
            break;
        default:
            break;
    }
}


- (void)backToHotCarDetail:(UIBarButtonItem *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title = @"车型详情";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:41/255.0 green:129/255.0 blue:255/255.0 alpha:1];
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];//取消系统默认返回按钮文字
    
//    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(backToHotCarDetail:)];
//    [backBtn setBackgroundImage:[UIImage imageNamed:@"back@2x.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backToHotCarDetail:)];
    self.navigationItem.leftBarButtonItem = backBtn;
    [backBtn release];
    
    
    self.tempView = nil;
    
    HotZhongShuViewController *hotZsVC = [[HotZhongShuViewController alloc] init];
    hotZsVC.mid = self.mid;
    [self changeView:hotZsVC];
    [hotZsVC release];
    
    NSArray *titles = [NSArray arrayWithObjects:@"综述",@"图片",@"资讯",@"活动",@"论坛", nil];
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:5];
    for (int i=0; i<titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [arr addObject:btn];
    }
    self.hotSectionView.buttons = arr;
    self.hotSectionView.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
